package com.yang.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.yang.baseservice.annotation.Log;
import com.yang.commonutils.R;
import com.yang.eduservice.entity.EduTeacher;
import com.yang.eduservice.entity.vo.TeacherQuery;
import com.yang.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author yds
 * @since 2020-11-18
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin //解决跨域
public class EduTeacherController {
    @Autowired
    private EduTeacherService  teacherService;

    /**
     * 获取所有讲师
     * @return
     */
    @ApiOperation("查询所有讲师")
    @GetMapping("findAll")
    @Log(value = "CQNX")
    public R findAllTeather(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    /**
     * 逻辑删除讲师
     * @param id
     * @return
     */
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("{id}")
    @Log(value = "AML")
    public R removeTeacher(@ApiParam(name="id",value="讲师id",required = true)
                                     @PathVariable String id){
        int a=10/0;
        boolean flag = teacherService.removeById(id);
        return flag?R.ok():R.error();

    }

    @ApiOperation("分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name="current",value="当前页",required = true)
                             @PathVariable long current,
                             @ApiParam(name="limit",value="每页数据",required = true)
                             @PathVariable long limit){

        //创建page对象
        Page<EduTeacher> pageTeacher=new Page<EduTeacher>(current,limit);
        teacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);

    }

    /***
     * 条件分页查询
     * @param current
     * @param limit
     * @param teacherQuery
     * @RequestBody 将post请求的json分装到注解对应的实体里
     * @return
     */
    @ApiOperation("讲师条件查询分页")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name="current",value="当前页",required = true)
                             @PathVariable long current,
                             @ApiParam(name="limit",value="每页数据",required = true)
                             @PathVariable long limit,
                             @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> pageTeacher=new Page<EduTeacher>(current,limit);
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<EduTeacher>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        IPage<EduTeacher> page = teacherService.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);

    }
    @ApiOperation("增加讲师接口")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        return save?R.ok():R.error();
    }
    @ApiOperation("根据id获取讲师信息")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher techer = teacherService.getById(id);
        return R.ok().data("item",techer);
    }
    @ApiOperation("修改讲师信息")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        return b?R.ok():R.error();
    }


}

